﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FB.Bussiness;
using FB.DataAccess;
using FB.Utilities;

namespace FB.admin
{
    public partial class ChanelCategoryModify : System.Web.UI.Page
    {
        /// <summary>
        ///  View State EditItemID
        /// </summary>
        private Guid? EditItemID
        {
            get
            {
                object obj = this.ViewState["_EditItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_EditItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadForm();
            }
        }
        private void CheckPermission()
        {
            if (Page.User.IsInRole(Constants.ROLE_ADMINISTRATOR)
                || Page.User.IsInRole(Constants.ROLE_AUTHOR)
                || Page.User.IsInRole(Constants.ROLE_CONTRIBUTER)
                || Page.User.IsInRole(Constants.ROLE_EDITOR))
            {
                //
            }
            else
            {
                Response.Redirect("/admin/AccessDenied.aspx");
            }
        }
        private void LoadForm()
        {
            Guid TempID ;
            if (Guid.TryParse(Request.QueryString["ID"], out TempID))
            {
                EditItemID = TempID;
            }
            if (EditItemID != null)
            {
                LoadFormEdit();
            }
            else
            {
                LoadFormAdd();
            }
        }


        private void LoadFormAdd()
        {
            lblTitle.Text = "Chanel: Add New";
        }

        private void LoadFormEdit()
        {
            lblTitle.Text = "Chanel: Edit";
            ChanelCategoryBO BO = new ChanelCategoryBO();
            ChanelCategory CurrentChanelCat = BO.GetChanelCategoryById((Guid)EditItemID);
            if (CurrentChanelCat != null)
            {
                txtName.Text = CurrentChanelCat.Name;
                txtDescription.Text = CurrentChanelCat.Description;
                chkEnable.Checked = (bool)CurrentChanelCat.Enable;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveForm();
            //TODO: Show msg
        }


        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveForm();
            Response.Redirect("ChanelCategoryList.aspx");
        }
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            SaveForm();
            EditItemID = null;
            Response.Redirect("ChanelCategoryModify.aspx?ID=0");
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChanelCategoryList.aspx");
        }
        private void SaveForm()
        {
            ChanelCategoryBO BO = new ChanelCategoryBO();
            //Edit

            if (EditItemID != null)
            {
                ChanelCategory EditChanelCat = BO.GetChanelCategoryById((Guid)EditItemID);
                EditChanelCat.Name = txtName.Text;
                EditChanelCat.Description = txtDescription.Text;
                EditChanelCat.Enable = chkEnable.Checked;
                BO.UpdateChanelCategory(EditChanelCat);
            }
            else
            {
                ChanelCategory NewChanelCate = new ChanelCategory();
                NewChanelCate.ID = Guid.NewGuid();
                NewChanelCate.Name = txtName.Text;
                NewChanelCate.Description = txtDescription.Text;
                NewChanelCate.Enable = chkEnable.Checked;
                BO.InsertChanelCategory(NewChanelCate);
            }
        }
    }
}